package com.rh.bmw4s.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.TextView;

import com.rh.bmw4s.R;
import com.rh.bmw4s.comm.ConfigValue;
import com.rh.bmw4s.utills.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 启动界面
 */
public class SplashActivity extends AppCompatActivity {

    @BindView(R.id.btn_splash_enter)
    TextView btnEnter;
    private int anInt;

    private Handler mhandler = new Handler();
    private Runnable mRunable;
    private static final int ANIM_TIME = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        anInt = SPUtils.getInstance().getInt(ConfigValue.userId, 0);

        //跳转到首页
        mRunable = new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent();
                if (anInt == 0) {
                    intent.setClass(SplashActivity.this, LoginRegActivity.class);
                } else {
                    intent.setClass(SplashActivity.this, MainActivity.class);
                }
                startActivity(intent);
                SplashActivity.this.finish();
            }
        };
        mhandler.postDelayed(mRunable, ANIM_TIME);
    }

    @OnClick(R.id.btn_splash_enter)
    public void onViewClicked() {
        mhandler.removeCallbacks(mRunable);
        Intent intent = new Intent();
        if (anInt == 0) {
            intent.setClass(SplashActivity.this, LoginRegActivity.class);
        } else {
            intent.setClass(SplashActivity.this, MainActivity.class);
        }
        startActivity(intent);
        SplashActivity.this.finish();
    }

    /**
     * 屏蔽物理返回按钮
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
